package com.sinodata.bsm.inmp.bean.gab;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.sinodata.bsm.common.vo.CollectField;
import com.sinodata.bsm.common.vo.Property;
import com.sinodata.bsm.common.vo.Res;
import com.sinodata.bsm.common.vo.ResPropertyValue;
import com.sinodata.bsm.common.vo.ResPropertyValue.PropValue;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-9-5 上午9:59:40          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class UPSBean extends WenshiduParentBean {

    private final static Long resTypeId = 6021000203000L;

    private final static int SIZE = 16;

    /**
     * propertyIdList用于存放所以的指标id
     * 
     * propIndexList用于存放每个指标在该类型上的索引值,和propertyIdList中的值一一对应
     */
    private final static List<Long> propertyIdList = new ArrayList<Long>();
    private final static List<Integer> propIndexList = new ArrayList<Integer>();

    /**
     * 以下为各指标对应的遥测表中的次序
     */
    private static final int batteryPercentageIndex = 2;//电池电量百分数
    private static final int inAElecticCurrentIndex = 6;//输入A相电流百分数
    private static final int inBElecticCurrentIndex = 7;//输入B相电流百分数
    private static final int inCElecticCurrentIndex = 8;//输入C相电流百分数
    private static final int systemTemperatureIndex = 11;//系统温度
    private static final int zhenliuTemperatureIndex = 13;//整流模块温度
    private static final int nibianTemperatureIndex = 14;//逆变模块温度
    private static final int outAElecticCurrentIndex = 22;//输出A相电流百分数
    private static final int outBElecticCurrentIndex = 23;//输出B相电流百分数
    private static final int outCElecticCurrentIndex = 24;//输出C相电流百分数
    private static final int nibianVoltageIndex = 26;//逆变器电压

    private static final int pangluJHIndex = 23;//远程旁路命令激活报警
    private static final int nibianKLIndex = 67;//逆变接触器开路状态
    private static final int pangluBHIndex = 68;//旁路接触器闭合状态
    private static final int nibianTIndex = 100;//逆变器HFDRV T信号状态
    private static final int nibianSXIndex = 101;//逆变器受限状态

    static {
        //初始化指标值
        for (int i = 1; i <= SIZE; i++) {
            propertyIdList.add(resTypeId * 100 + i);
        }

        //初始化指标在表中的索引
        propIndexList.add(batteryPercentageIndex);
        propIndexList.add(inAElecticCurrentIndex);
        propIndexList.add(inBElecticCurrentIndex);
        propIndexList.add(inCElecticCurrentIndex);
        propIndexList.add(systemTemperatureIndex);
        propIndexList.add(zhenliuTemperatureIndex);
        propIndexList.add(nibianTemperatureIndex);
        propIndexList.add(outAElecticCurrentIndex);
        propIndexList.add(outBElecticCurrentIndex);
        propIndexList.add(outCElecticCurrentIndex);
        propIndexList.add(nibianVoltageIndex);
        propIndexList.add(pangluJHIndex);
        propIndexList.add(nibianKLIndex);
        propIndexList.add(pangluBHIndex);
        propIndexList.add(nibianTIndex);
        propIndexList.add(nibianSXIndex);
    }

    public UPSBean(Res res) {
        if (!resTypeId.equals(res.getResTypeId())) {
            return;
        }
        this.res = res;
        this.deviceNumber = Integer.valueOf(res.getInternalId());
        init();
    }

    private void init() {
        for (int i = 0; i < 11; i++) {
            propValueMap.put(propertyIdList.get(i), this.getYcValueFromDataMap(propIndexList.get(i)));
        }
        for (int i = 11; i < SIZE; i++) {
            propValueMap.put(propertyIdList.get(i), this.getYXValueFromDataMap(propIndexList.get(i)));

        }
    }

    @Override
    public ResPropertyValue getPropertyValue() {
        ResPropertyValue value = new ResPropertyValue();
        value.setResId(res.getId());
        value.setTaskId(-2L);
        value.setTemporary(false);
        value.setTime(Calendar.getInstance().getTime());
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propertyIdList.size()];

        for (int i = 0; i < SIZE; i++) {
            Long propId = propertyIdList.get(i);
            Property property = propertyCache.get(propId);
            if (property == null) {
                continue;
            }
            CollectField field = fieldCache.getByPropId(propId);
            PropValue propValue = null;
            
            if(i==11){
            	if(propValueMap.get(propId).toString().equals("0")){
            		propValue = ResPropertyValue.createPropValue(propId, "解除", field.getFieldName());
            	}else{
            		propValue = ResPropertyValue.createPropValue(propId, "报警", field.getFieldName());
            	}
            }else if(i>11){
            	if(propValueMap.get(propId).toString().equals("0")){
            		propValue = ResPropertyValue.createPropValue(propId, "关闭", field.getFieldName());
            	}else{
            		propValue = ResPropertyValue.createPropValue(propId, "运行", field.getFieldName());
            	}            
            }else{
            	propValue = ResPropertyValue.createPropValue(propId, propValueMap.get(propId).toString(), field.getFieldName());
            }
            propValues[i] = propValue;
        }
        value.setValues(propValues);
        return value;
    }

}
